<?php

session_start();

// Include i file necessari
include('inc/config.inc.php');
include('inc/template.inc.php');
include('inc/db.inc.php');
include('inc/check.inc.php');

// Variabili
$list = "";

// Recupera la lista dei presenti e la loro locazione
if($r1 = $db -> query("SELECT user_id, user_name, user_surname, user_guild, user_guildrank, user_race, user_sex, user_loc FROM users WHERE user_lasttime > (NOW() - INTERVAL 5 MINUTE) ORDER BY user_name ASC"))
{
	while($rd1 = $r1 -> fetch_assoc())
	{
		// Variabili
		$user_id		= trim(htmlentities($rd1['user_id']));
		$user_name		= trim(htmlentities($rd1['user_name']));
		$user_surname	= trim(htmlentities($rd1['user_surname']));
		$user_loc		= trim(htmlentities($rd1['user_loc']));
		$guild_id		= trim(htmlentities($rd1['user_guild']));
		$race_id		= trim(htmlentities($rd1['user_race']));
		$rank_id		= trim(htmlentities($rd1['user_guildrank']));
		
		// Recupera le informazioni sulla razza
		if($r2 = $db -> query("SELECT race_image, race_name FROM races WHERE race_id = '$race_id'"))
		{
			$rd2 = $r2 -> fetch_assoc();
			
			$race_img	= trim(htmlentities($rd2['race_image']));
			$race_name	= htmlentities($rd2['race_name']);
		}
		// Recupera le informazioni sul rango
		if($r3 = $db -> query("SELECT rank_image, rank_name FROM guildranks WHERE rank_id = '$rank_id'"))
		{
			$rd3 = $r3 -> fetch_assoc();
			
			$rank_img	= trim(htmlentities($rd3['rank_image']));
			$rank_name	= trim(htmlentities($rd3['rank_name']));
		}
		// Recupera le informazioni sulla gilda
		if($r5 = $db -> query("SELECT guild_name FROM guilds WHERE guild_id = '$guild_id'"))
		{
			$rd5 = $r5 -> fetch_assoc();
			
			$guild_name = trim(htmlentities($rd5['guild_name']));
		}
		// Recupera la locazione del personaggio
		if($r6 = $db -> query("SELECT loc_name FROM locations WHERE loc_id = '$user_loc'")) 
		{
			$rd6 = $r6 -> fetch_assoc();
			
			$loc_name = trim(htmlentities($rd6['loc_name']));			
		}
	
			
		// Costruisce la lista dei presenti
		$list .= '<tr><td><a href="avatar.php?id=' . $user_id . '" target="_self">' . $user_name . ' ' . $user_surname . '</a></td>';
		$list .= '<td><img src="img/races/' . $race_img . '" width="20px" height="20px" title="' . $race_name . '" /></td>';
		$list .= '<td><img src="img/guilds/' . $rank_img . '" width="20px" height="20px" title="' . $rank_name . ' di ' . $guild_name . '" /></td>';
		$list .= '<td><a href="game.php?loc=' . $user_loc . '" target="_top">' . $loc_name . '</a></div><div class="clear"></td></tr>';
	}

	// Lista delle tag speciali
	$tags = array('temp_title' => $g_name, 'temp_list' => $list);

	// Carica il template e formatta le tag speciali
	$temp = new template('style/online.template.html');
	$temp -> replace($tags);

	// Visualizza l'output
	echo $temp -> show();

	$r1 -> close();
}

// Chiude la connessione al database
$db -> close();

?>